package getproblem

import (
	"errors"
	"fmt"
	"myOJ/datastructure"
	"myOJ/db"
	"time"
)

func GetProblem(title string) (datastructure.ProblemDetail, error) {
	var pd datastructure.ProblemDetail
	//取问题内容
	rows, err := db.Dd.Query("SELECT * FROM problems WHERE title='" + title + "'")
	if err != nil {
		fmt.Println("GetProblem table problems", err)
		return pd, errors.New("GetProblem Error")
	}

	var id int
	var ptitle, content, poster, answercode string
	var pushtime time.Time
	if rows.Next() {
		rows.Scan(&id, &ptitle, &content, &pushtime, &poster, &answercode)
	}

	//取样例
	testrows, err := db.Dd.Query("SELECT * FROM tests WHERE title='" + title + "'")
	if err != nil {
		fmt.Println("GetProblem table tests", err)
		return pd, errors.New("GetProblem Error")
	}

	testcases := make(map[string]string, 2)
	var ttitle, input, output string
	count := 0
	for testrows.Next() {
		testrows.Scan(&id, &ttitle, &input, &output)
		testcases[input] = output
		count++
		if count >= 2 {
			break
		}
	}
	pd = datastructure.ProblemDetail{id, title, content, testcases}
	return pd, nil
}
